<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目名称" prop="name">
        <el-select v-model="queryParams.projectId" clearable collapse-tags size="small" placeholder="请输入项目名称" @change="handleQuery">
          <el-option v-for="project in projectDatas"
                       :key="project.id" :label="project.projectName" :value="parseInt(project.id)" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="开始日期">
        <el-date-picker v-model="dateRangeDoTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="结束日期">
        <el-date-picker v-model="dateRangeEndTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="结束日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="一级分类" prop="doTaskUid">
        <el-select v-model="queryParams.maintainId" placeholder="请选择一级分类" clearable  size="small" @change="changeFirst">
          <el-option v-for="dict in firstLevelDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" prop="projectId">
        <el-select v-model="queryParams.subMaintainId" placeholder="请选择二级分类" clearable  size="small" @change="handleQuery" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in secondLevelDatas"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目经理" prop="projectLeaderUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.projectLeaderUid" :multiple="true" placeholder="请选择项目经理" roleId="11" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目主管" prop="directorUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.directorUid" :multiple="true" placeholder="请选择项目主管" roleId="12" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="区域经理" prop="areaManagerUid">
        <user-select ref="UserSelect" :pModel.sync="queryParams.areaManagerUid" :multiple="true" placeholder="请选择区域经理" roleId="10" :selectChange="handleQuery" @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['fi:coreTask:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="index" fixed="left" type="index" :index="showRn" width="60" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="完成数量" align="center" prop="itemCount" >
      </el-table-column>
      <el-table-column label="项目主管" align="center" prop="directorName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import { listTree } from "@/api/system/dict/data";
import UserSelect from '@/components/UserSelect';
import { DICT_TYPE } from '@/utils/dict';
import {getProjectPage, getTaskType, getTaskDate} from '@/api/fi/statistics';
import {getProjectList} from '@/api/fi/project';
export default {
  name: "ProjectStatistics",
  components: {
    UserSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目反馈列表
      list: [],
      // 是否显示弹出层
      open: false,
      dateRangeEndTime: [],
      dateRangeDoTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        maintainId: null,
        doTaskUid: null,
        projectId: null,
        isFinish: null,
        doTaskDesc: null,
        projectLeaderUid: null,
        areaManagerUid : null,
        directorUid : null
      },
      firstLevelDatas : [],
      secondLevelDatas : [],
      projectDatas : []
    };
  },
  created() {
    this.getList();
    this.getMaintainOptions();
    this.getProjectDatas();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeDoTime, 'doTime');
      // 执行查询
      getProjectPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        
      }); 
      this.loading = false;
    },
    //获取项目下拉框
    getProjectDatas(){
      var params = {};
      // 执行查询
      getProjectList(params).then(response => {
        this.projectDatas = response.data;
      });
    },
    //获取一级分类
    getMaintainOptions(){
      const params = {
        dictType : DICT_TYPE.FI_PROJECT_FEEDBACK_TYPE
      }
      listTree(params).then(response => {
        this.firstLevelDatas = response.data;
      });
    },
    changeFirst(val){
      let node = this.firstLevelDatas.find(item => item.id===val);
      this.secondLevelDatas = node.children;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.subMaintainId){
          this.queryParams.maintainId = this.queryParams.subMaintainId;
      }
      this.queryParams.pageNo = 1;
      this.getList();
    },
    handleDetail(row){
      this.$router.push({path: "/fi/project/detailTab/" + row.projectId});
    },
    showRn(index){
      return index+this.queryParams.pageSize*(this.queryParams.pageNo -1) + 1
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      this.addBeginAndEndTime(params, this.dateRangeFinishDate, 'finishDate');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeDoTime, 'doTime');
      // 执行导出
      this.$confirm('是否确认导出所有项目反馈数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportExcel(params);
        }).then(response => {
          this.downloadExcel(response, '项目反馈.xls');
        })
    }
  }
};
</script>
